package handlers

import (
	"runtime"
	"github.com/nsqio/go-nsq"
	"generalhandler/conf"
	"fmt"
)

func Launch(handler Handler) {
	runtime.GOMAXPROCS(runtime.NumCPU())

	c, err := nsq.NewConsumer(conf.MQConfigInstance().MQTopic, conf.MQConfigInstance().MQChannel, nsq.NewConfig())
	if nil != err {
		panic(fmt.Sprintf("failed to new consumer, cuz %s", err.Error()))
	}

	c.AddHandler(MQHandlerInstance(handler))

	if err := c.ConnectToNSQDs(conf.MQConfigInstance().NsqdTcpAddresses); nil != err {
		panic(fmt.Sprintf("failed to connect to nsqd, cuz %s", err.Error()))
	}

	//if err := c.ConnectToNSQLookupds(conf.MQConfigInstance().NsqLookupdAddresses); nil != err {
	//	panic(fmt.Sprintf("failed to connect to nsqlookup, cuz %s", err.Error()))
	//}

	<-c.StopChan
}
